package com.example.springbootdemo.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.example.springbootdemo.bean.Bean1;
import com.example.springbootdemo.bean.Bean2;
import com.example.springbootdemo.bean.Bean3;
import com.example.springbootdemo.bean.Bean4;
import com.example.springbootdemo.bean.child.Bean01;
import com.example.springbootdemo.mapper.Mapper1;
import com.example.springbootdemo.mapper.Mapper2;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ComponentScan("com.example.springbootdemo.bean.child")
public class Config {
  @Bean
  public Bean1 bean1(){
    return new Bean1();
  }
  @Bean
  public Bean2 bean2(){
    return new Bean2();
  }
  @Bean
  public Bean3 bean3(){
    return new Bean3();
  }
  @Bean
  public Bean4 bean4(){
    return new Bean4();
  }

  public Bean01 bean01(){
    return new Bean01();
  }

  @Bean
  public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
    SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
    factoryBean.setDataSource(dataSource);
    return factoryBean;
  }

  @Bean(initMethod = "init")
  public DruidDataSource dataSource(){
    DruidDataSource source = new DruidDataSource();
    source.setUrl("jdbc:mysql://localhost:3306/test");
    source.setUsername("root");
    source.setPassword("12345678");
    return source;
  }

//  @Bean
//  public MapperFactoryBean<Mapper1> mapper1(SqlSessionFactory sqlSessionFactory){
//    MapperFactoryBean<Mapper1> factoryBean = new MapperFactoryBean<>(Mapper1.class);
//    factoryBean.setSqlSessionFactory(sqlSessionFactory);
//    return factoryBean;
//  }
//
//  @Bean
//  public MapperFactoryBean<Mapper2> mapper2(SqlSessionFactory sqlSessionFactory){
//    MapperFactoryBean<Mapper2> factoryBean = new MapperFactoryBean<>(Mapper2.class);
//    factoryBean.setSqlSessionFactory(sqlSessionFactory);
//    return factoryBean;
//  }
}
